<!--
 * @Author: liaoxing
 * @Date: 2021-10-22 16:47:06
 * @LastEditors  : maps131_liaoxing
 * @LastEditTime : 2022-03-15 16:59:39
 * @Description: liaoxing created
 * @FilePath     : \pin-xin-admin\src\views\process\category\index.vue
-->
<template>
  <div class="createProcessCategory">
    <div class="searchBox">
      <el-form :inline="true" :model="searchData" class="formBox">
        <el-form-item label="关键词">
          <el-input v-model="searchData.keyWord" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button class="addCategory" type="primary" @click="operateCategory(false)">新增</el-button>
    </div>
    <div class="tableBox">
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        style="width: 100%;max-height: 660px;overflow: auto">
        <el-table-column
          prop="name"
          label="类别名称"
          width="300">
        </el-table-column>
        <!-- <el-table-column
          prop="code"
          label="编码"
          width="200">
        </el-table-column> -->
        <el-table-column
          prop="created"
          label="创建时间"
          width="250">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改" placement="bottom">
              <span class="mlr5 finger" @click="operateCategory(true, scope.row)">
                <i class="el-icon-edit-outline font18"></i>
              </span>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
              <span class="mlr5 finger">
                <el-popconfirm
                  title="确定要删除该分类吗？"
                  @confirm="deleteCategory(scope.row)"
                >
                  <i slot="reference" class="el-icon-delete color-red font18"></i>
                </el-popconfirm>
              </span>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="float: right; margin-top: 20px"
        layout="prev, pager, next"
        @current-change="switchPage"
        :total="totleNum">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" width="30%">
      <el-form :label-position="'right'" label-width="80px" :model="itemInfo" ref="itemInfo" :rules="rules">
        <el-form-item label="类别名称" prop="name">
          <el-input v-model.trim="itemInfo.name"></el-input>
        </el-form-item>
        <!-- <el-form-item label="编码" prop="code">
          <el-input v-model.trim="itemInfo.code" :disabled="isEdit"></el-input>
        </el-form-item> -->
        <el-form-item label="描述">
          <el-input type="textarea" v-model.trim="itemInfo.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('itemInfo')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// import checkValue from "@/utils/checkCode"
export default {
  name: 'createProcessCategory',
  data () {
    // let checkCode = async (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error('编码不能为空'));
    //   }
    //   let flag = await this.checkEntityExiste(value)
    //   if (!flag) {
    //     return callback(new Error('编码重复，请重新输入'));
    //   } else {
    //     let status = checkValue(value)
    //     if (!status) {
    //       callback()
    //       return
    //     }
    //     return callback(new Error(status));
    //   }
    // }
    return {
      loading: false,
      dialogFormVisible: false,
      searchData: {
        keyWord: ''
      },
      tableData: [],
      itemInfo: {
        name: "",
        code: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: '请输入类别名称', trigger: 'blur' },
        ],
        // code: [
        //   { required: true, validator: checkCode, trigger: 'blur' },
        // ]
      },
      pageSize: 14,
      pageNum: 1,
      isEdit: false,
      totleNum: 0
    }
  },
  watch: {
    dialogFormVisible (value) {
      if (!value) {
        this.isEdit = false
        this.$refs["itemInfo"].clearValidate()
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /* 重置 */
    reset () {
      this.searchData.keyWord = ''
      this.getList()
    },
    onSubmit () {
      if (!this.searchData.keyWord) return
      this.getList()
    },
    /* 切换分页 */
    switchPage (num) {
      this.pageNum = num
      this.getList()
    },
    /* 获取列表 */
    getList () {
      this.loading = true
      let params = {
        pageSize: this.pageSize,
        pageNum: this.pageNum
      }
      if (this.searchData.keyWord) {
        params.keyWord = this.searchData.keyWord
      }
      this.$serve.getCategories(params)
        .then(res => {
          this.loading = false
          if (res.code === 200) {
            this.tableData = res.data.records
            this.totleNum = res.data.total
          } else {
            this.$message.error("请求异常！" + res.msg)
          }
        })
    },
    /* 删除分类 */
    deleteCategory (item) {
      this.$serve.delete(item.id)
        .then(res => {
          if (res.code === 200) {
            this.getList()
          } else {
            this.$message.error("请求异常！" + res.msg)
          }
        })
    },
    /* 新增或者修改 */
    operateCategory (type, para) {
      this.isEdit = type
      this.dialogFormVisible = true
      if (type) { // 修改
        this.isEdit = true
        this.itemInfo = JSON.parse(JSON.stringify(para))
        return
      }
      this.itemInfo = {
        name: "",
        code: "",
        description: "",
      }
    },
    /* 提交新增或者修改 */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$serve.saveOrUpdate(this.itemInfo, this.isEdit, this.itemInfo.id)
          .then(res => {
            if (res.code === 200) {
              this.dialogFormVisible = false
              this.getList()
            } else {
              this.$message.error("请求异常！" + res.msg)
            }
          })
        } else {
          return false;
        }
      });
    },
    /* 检测编码是否重复 */
    // checkEntityExiste (value) {
    //   if (this.isEdit) return true
    //   return new Promise((res) => {
    //     let params = {
    //       field: "code",
    //       fieldName: "编码",
    //       fieldValue: value,
    //       id: ""
    //     }
    //     this.$serve.checkEntityExiste(params)
    //       .then(data => {
    //         res(data.data)
    //       })
    //   })
    // }
  }
}
</script>

<style lang="less">
.createProcessCategory{
  width: 100%;
  height: calc(100vh - 85px);
  box-sizing: border-box;
  padding: 20px;
  background-color: whitesmoke;
  .searchBox{
    height: 60px;
    background-color: white;
    display: flex;
    align-items: center;
    position: relative;
    margin-bottom: 20px;
    .formBox{
      height: 40px;
      margin-left: 40px;
    }
    .addCategory{
      position: absolute;
      right: 40px;
    }
  }
  .tableBox{
    height: calc(100% - 90px);
    background-color: white;
    padding: 10px;
  }
}
</style>